<?php  
require '../../frame/base.php';

$tag = !empty($_REQUEST['tag'])?$_REQUEST['tag']:null;
$sid = !empty($_REQUEST['sid'])?$_REQUEST['sid']:null;
$uid = !empty($_REQUEST['uid'])?$_REQUEST['uid']:null;

$_REQUEST['page'] = !empty($_REQUEST['page'])?$_REQUEST['page']:'';
$_REQUEST['pagesize'] = !empty($_REQUEST['pagesize'])?$_REQUEST['pagesize']:'';

// 分页
if($_REQUEST['page']!="" && $_REQUEST['pagesize']!=""){
	$de=$_REQUEST['page']*$_REQUEST['pagesize'];
	$size=$_REQUEST['pagesize'];
}else{
	$de=0;
	$size=20;
}
// 分页 end


if($tag=='all'){
	// 平台优惠券
	$pin=$Db->table('coupon')->where('shopid','0')->limit($de,$size)->select();
//计算参数
if($pin){
	foreach($pin as $k=>$v){
		$data['pin'][$k]=array(
			'couponid'=>$v['couponid'],
			'srcimages'=>$v['srcimages'],
			'images'=>$v['images'],
			'title'=>$v['title'],
			'top_price'=>$v['top_price'],
			'price'=>$v['price'],
			'number'=>$v['number'],
			'startdate'=>$v['startdate'],
			'enddate'=>$v['enddate'],
			'enddate'=>$v['enddate'],
			'jianjie'=>$v['jianjie'],
		);
		if($v['categroyids']!=''){
			$cateid['cateid']=array('in',$v['categroyids']);
			$categroy=$Db->table('categroy')->where($cateid)->select();
			foreach($categroy as $vs){
				$pins[]=$vs['name'];
			}
			$data['pin'][$k]['use']=implode(',',$pins);
			if($Db->table('mycoupon')->where(['userid'=>$uid])->where('couponid',$v['couponid'])->find()){
				$data['pin'][$k]['isGet']=1;
			}else{
				$data['pin'][$k]['isGet']=0;
			}
		}else{
			if($Db->table('mycoupon')->where(['userid'=>$uid])->where('couponid',$v['couponid'])->find()){
				$data['pin'][$k]['isGet']=1;
			}else{
				$data['pin'][$k]['isGet']=0;
			}
			$data['pin'][$k]['use']='全平台适用';
		}
	}
}else{
	$data['pin']=[];
}
	
	// 商家优惠券
	$data['shop']=$Db->table('coupon')->field('couponid,srcimages,images,title,top_price,price,number,startdate,enddate,jianjie')->where('state',1)->where('shopid',$sid)->limit($de,$size)->select();
if($data['shop']){
	foreach($data['shop'] as $k=>$v){
		if( $Db->table('mycoupon')->where(['userid'=>$uid])->where('couponid',$v['couponid'])->find() ){
			$data['shop'][$k]['isGet']=1;
		}else{
			$data['shop'][$k]['isGet']=0;
		}
	}
}
else{
	$data['shop']=[];
}

echo json_encode(['code'=>200,'msg'=>'成功','data'=>$data]);
}
